<template>
  <a-dropdown v-if="currentUser && currentUser.fullname" placement="bottomRight">
    <span class="ant-pro-account-avatar">
      <a-avatar
        v-if="currentUser.avatar"
        size="default"
        :src="currentUser.avatar"
        class="antd-pro-global-header-index-avatar"
      />
      <img v-else src="@/assets/images/defaultAva.png" alt="" class="defaultAvatar" />
      <span>{{ currentUser.fullname }}</span>
    </span>
    <template v-slot:overlay>
      <a-menu class="ant-pro-drop-down menu" :selected-keys="[]">
        <a-menu-item v-if="menu" key="pwd" @click="handleToSettings">
          <a-icon type="setting" />
          修改密码
        </a-menu-item>
        <a-menu-divider />
        <a-menu-item key="logout" @click="handleLogout">
          <a-icon class="logout" type="logout" />
          退出登录
        </a-menu-item>
        <a-menu-divider />
        <a-menu-item @click="toVisualization">
          <a-icon class="visualization" type="bar-chart" />
          数据可视化
        </a-menu-item>
        <a-menu-divider />
        <a-menu-item @click="isShow">
          <a-icon type="setting" />
          设置
        </a-menu-item>
      </a-menu>
    </template>
  </a-dropdown>
  <span v-else>
    <a-spin size="small" :style="{ marginLeft: 8, marginRight: 8 }" />
  </span>
</template>

<script>
import { Modal } from 'ant-design-vue'
import storage from 'store'
import { ACCESS_TOKEN, APP_USER, APP_DEPT, SFBI_URL } from '@/store/mutation-types'
import { ADMREVIEW_DEPART, CORRECTION_DEPART } from '@/utils/custom/custom-depart.js'
import { ACCEPT_ROUTER, CORRECTION_ROUTER } from '@/utils/custom/custom-bi.js'

export default {
  name: 'AvatarDropdown',
  props: {
    menu: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      currentUser: storage.get(APP_USER), // 登录人信息
      currentDept: storage.get(APP_DEPT) // 登录人部门
    }
  },
  methods: {
    // 跳转到大屏页面
    toVisualization () {
      if (ADMREVIEW_DEPART.includes(this.currentDept.sectionid)) {
        // 行政复议大屏
        window.location.href = SFBI_URL + ACCEPT_ROUTER
      } else if (CORRECTION_DEPART.includes(this.currentDept.sectionid)) {
        // 社区矫正大屏
        window.location.href = SFBI_URL + CORRECTION_ROUTER
      } else {
        console.error('未配置大屏跳转路由', this.currentDept.id)
      }
    },
    // 修改密码
    handleToSettings () {
      this.$router.push({ name: 'ha_modifyInfo' })
    },
    // 退出登录
    handleLogout () {
      Modal.confirm({
        title: '提示',
        content: '确认要退出系统吗？',
        onOk: () => {
          return this.$store.dispatch('Logout').then(() => {
            // 清除token信息
            storage.remove(ACCESS_TOKEN)
            // 清除用户信息
            storage.remove(APP_USER)
            storage.remove(APP_DEPT)
            this.$router.push({ path: '/logout' })
          })
        },
        onCancel () {}
      })
    },
    isShow () {
      this.$EventBus.$emit('Myevent', true)
    }
  }
}
</script>

<style lang="less" scoped>
.ant-pro-drop-down {
  /deep/ .action {
    margin-right: 8px;
  }
  /deep/ .ant-dropdown-menu-item {
    min-width: 160px;
  }
}
.defaultAvatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  margin-right: 10px;
}

.logout {
  color: red;
}
.visualization {
  color: rgb(0, 153, 255);
}
</style>
@/utils/custom/custom-bi.js
